#include <allegro5/allegro.h>
#include <allegro5/allegro_primitives.h>

#include "TankFactory.h"
#include "Tank.h"

const unsigned int kWidth = 800;
const unsigned int kHeight = 600;

int main(int argc, char **argv) {
    ALLEGRO_DISPLAY *display = NULL;
    
    if(!al_init()) {
        fprintf(stderr, "failed to initialize allegro!\n");
        return -1;
    }
    
    al_init_primitives_addon();
    
    // Display setup
    al_set_new_display_flags( ALLEGRO_WINDOWED );
    display = al_create_display(kWidth, kHeight);
    if(!display) {
        fprintf(stderr, "failed to create display!\n");
        return -1;
    }
    
    al_clear_to_color(al_map_rgb(0,0,0));
    
    // ==========================
    _Tank tank( TankFactory::BuildBase1Tank() );
    
    //BaseComponent::point centerPoint = {400, 300};
    BaseComponent::point currentPoint;
    
    ALLEGRO_TRANSFORM T;
	
	for ( float c = 0.0, t = -1.57; c < 6.29; c += -0.017, t += -0.017  ) {
        al_identity_transform( &T );
        al_translate_transform( &T, 0.0, -100.0 );
        al_rotate_transform( &T, c );
        al_translate_transform( &T, 400.0, 300.0 );
        currentPoint.x = currentPoint.y = 0;
        al_transform_coordinates( &T, &currentPoint.x, &currentPoint.y );
        tank->MoveTo( currentPoint );
        
		al_clear_to_color(al_map_rgb(0,0,0));
		tank->RotateTo( t );
		tank->RotateTurretTo( 0, -3.14-c );
		tank->Draw();
		al_flip_display();
		al_rest(0.02);
	}
    
    
    // ==========================
    
    al_flip_display();
    
    al_rest(2.0);
    
    al_destroy_display(display);
    
    return 0;
}
